﻿using System;
using System.Collections.Generic;
using System.Web.UI;
using Facade;
using IronOne.SalesOne.BusinessObjects;

namespace IronOne.SalesOne.SalesOneWeb.Collection
{
    public partial class SearchCollectionUploadedReport : SOSecurityPage
    {
        StructureFacade facade;
        int userType;
        int userId;
        private static int pageNo = 1;
        UserInformation info;

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                facade = new StructureFacade();

                if (!Page.IsPostBack)
                {
                    txtFrom.Text = DateTime.Today.ToShortDateString();
                    txtTo.Text = DateTime.Today.ToShortDateString();

                    ValidateUserCredentials(Request.Url.LocalPath);

                    User user = facade.GetUser(User.Identity.Name);
                    UserInformation info = facade.GetUserInformation(User.Identity.Name);
                    userType = user.UserType;
                    userId = user.NodeId;

                    if (userType == 1)
                    {
                        try
                        {
                            FillDropDown("Branch");
                        }
                        catch (Exception ez) { }
                    }
                    else if (userType == 2)
                    {
                        try
                        {
                            FillDropDown("BranchGrp");
                        }
                        catch (Exception ex) { }
                    }
                    else if (userType == 3)
                    {
                        try
                        {
                            UserInBranch();
                        }
                        catch (Exception ex) { }
                    }
                }
            }
            catch (Exception ex) { }
        }


        public void FillDropDown(string type)
        {
            switch (type)
            {
                case "BranchGrp":

                    List<Branch> br1 = facade.GetBranchesForGroup(userId);

                    Branch branch1 = new Branch();
                    branch1.BranchId = -1;
                    branch1.BranchName = "Select...";
                    br1.Insert(0, branch1);
                    ddlBranch.DataSource = br1;
                    ddlBranch.DataTextField = "BranchName";
                    ddlBranch.DataValueField = "BranchId";
                    ddlBranch.DataBind();
                    break;

                case "Branch":
                    List<Branch> br = facade.GetBranches("BranchName ASC");
                    Branch branch = new Branch();
                    branch.BranchId = -1;
                    branch.BranchName = "Select...";
                    br.Insert(0, branch);
                    ddlBranch.DataSource = br;
                    ddlBranch.DataTextField = "BranchName";
                    ddlBranch.DataValueField = "BranchId";
                    ddlBranch.DataBind();
                    break;

                default:
                    break;
            }
        }


        public void UserInBranch()
        {
            User user = facade.GetUser(User.Identity.Name);
            UserInformation info = facade.GetUserInformation(User.Identity.Name);
            userType = user.UserType;

            FillDropDown("Branch");
            ddlBranch.SelectedValue = info.NodeId.ToString();

            //drpBranch.SelectedValue = user.NodeId.ToString();
            ddlBranch.Enabled = false;
        }


        protected void btnSearch_Click(object sender, EventArgs e)
        {
            try
            {
                FinanceFacade fac = new FinanceFacade();
                StructureFacade sFac = new StructureFacade();

                if (ddlBranch.SelectedValue == "-1")
                {
                    MassageSummery("IIS503");
                }
                else if ((Convert.ToDateTime(txtTo.Text).Day - Convert.ToDateTime(txtFrom.Text).Day) > 3)
                {
                    MassageSummery("IIS522");
                }
                else if (Convert.ToDateTime(txtFrom.Text) > System.DateTime.Today)
                {
                    MassageSummery("IIS521");
                }
                else
                {
                    int branchID = sFac.GetBranchByName(ddlBranch.SelectedItem.ToString()).BranchId;
                    List<CollectionTransaction> coList = fac.GetCollectionByBranch(branchID, Convert.ToDateTime(txtFrom.Text), Convert.ToDateTime(txtTo.Text).AddDays(1));
                    if (coList.Count != 0)
                    {
                        //Response.Redirect("~/Collection/ViewReportCollection.aspx?branchCode=" + branchCode + "&FromDate=" + Convert.ToDateTime(txtFrom.Text) + "&ToDate=" + Convert.ToDateTime(txtTo.Text));

                        string url = "../Collection/ViewReportUploadedCollection.aspx?branchCode=" + branchID + "&FromDate=" + Convert.ToDateTime(txtFrom.Text).ToShortDateString() + "&ToDate=" + Convert.ToDateTime(txtTo.Text).AddDays(1).ToShortDateString();
                        string fullURL = "window.open('" + url + "', '_blank', 'height=1000,width=1000,status=yes,toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=no,titlebar=no' );";
                        ScriptManager.RegisterStartupScript(this, typeof(string), "OPEN_WINDOW", fullURL, true);
                    }
                    else
                    {
                        MassageSummery("IIS504");
                    }
                }
                // List<CollectionTransaction> colList = fac.GetCollectionByBranch();
            }
            catch (Exception ex)
            {
            }
        }
    }
}
